{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3f4887d6-e963-4f30-88aa-e38f4caf087b",
   "metadata": {},
   "source": [
    "Runtime: 680 ms, faster than 73.29% of Python3 online submissions for Maximum Subarray.\n",
    "Memory Usage: 28 MB, less than 70.70% of Python3 online submissions for Maximum Subarray."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32dbde54-c290-46d9-89b0-eb5e7bf81460",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def maxSubArray(self, nums: List[int]) -> int:\n",
    "        def find_max_sum_subarray(array):\n",
    "            # chunks[i] = sum of the optimal chunk ending at array[i].\n",
    "            chunks = []\n",
    "            for item in array:\n",
    "                if not chunks:\n",
    "                    # Start a new chunk containing the first element.\n",
    "                    chunks.append(item)\n",
    "                else:\n",
    "                    if chunks[-1] < 0:\n",
    "                        # Start a new chunk.\n",
    "                        chunk = item\n",
    "                    else:\n",
    "                        # Grow the previous chunk.\n",
    "                        chunk = chunks[-1] + item\n",
    "                    chunks.append(chunk)\n",
    "            # Choose the best chunk.\n",
    "            return max(chunks) if chunks else 0\n",
    "        \n",
    "        return find_max_sum_subarray(nums)\n",
    "        \n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "        \"\"\"\n",
    "        if len(nums) == 0:\n",
    "            return 0\n",
    "        if len(nums) == 1:\n",
    "            return nums[0]\n",
    "\n",
    "        self.current_max = float(\"-inf\")\n",
    "    \n",
    "        def computation(index: int, sum_: int) -> int:\n",
    "            if (index > len(nums)-1):\n",
    "                #the end of the list, return\n",
    "                return\n",
    "            \n",
    "            new_value = sum_ + nums[index]\n",
    "            if new_value > self.current_max:\n",
    "                self.current_max = new_value\n",
    "            \n",
    "            computation(index+1, new_value)\n",
    "        \n",
    "        for i, num in enumerate(nums):\n",
    "            if num > self.current_max:\n",
    "                self.current_max = num\n",
    "            \n",
    "            computation(i+1, num)\n",
    "        \n",
    "        return self.current_max\n",
    "        \"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
